jQuery(function() {
    if (jQuery('.package-status-change-action').length > 0) {
        jQuery('.package-status-change-action').bind('click', function() {
            var oid = jQuery(this).attr('package_name');
            var status = jQuery(this).attr('status');
            var text = '';
            if (status == 1) {
                text = 'Change status Package "' + oid + '" to Invoice Sent ?';
            }
            if (status == 3) {
                text = 'Change status Package "' + oid + '" to Shipped ?';
            }
            if (text != '') {
                if (confirm(text)) {
                    window.location.href = jQuery(this).attr('href');
                }
            }
            return false;
        });
    }
    if (jQuery('.orders_table_data').length > 0) {
        jQuery('.orders_table_data').each(function() {
            var table_id = jQuery(this).attr('id');
            window[table_id] = jQuery('#' + table_id).dataTable({
                "iDisplayLength": 20,
                "aLengthMenu": false,
                "sPaginationType": "full_numbers"
            });
            if (jQuery(this).hasClass('not-filter')) {
                jQuery('#' + table_id + '_filter').hide();
            }
        });
    }
    tvn_order_bind_edit_action();
});
function tvn_order_edit_validate(e_class) {
    jQuery('td label.error').remove();
    var pass = true;
    jQuery('.' + e_class).each(function() {
        var $this = jQuery(this);
        if ($this.hasClass('required')) {
            if ($this.val() == '') {
                pass = false;
                $this.parent().prepend('<label class="error">Field is required!</label>');
            }
        }
        if ($this.hasClass('numeric') && $this.val() != '') {
            if (!jQuery.isNumeric($this.val())) {
                pass = false;
                if ($this.parent().find('label.error').length <= 0) {
                    $this.parent().prepend('<label class="error">Field must is numeric!</label>');
                }
            }
        }
    });
    return pass;
}
function tvn_order_edit_submit(e_class) {
    var query_str = '';
    var oid = 0;
    jQuery('.' + e_class).each(function() {
        if (jQuery(this).attr('name') == 'oid') {
            oid = jQuery(this).val();
        }
        query_str += jQuery(this).attr('name') + '=' + jQuery(this).val() + '&';
    });
    jQuery.post(Drupal.settings.basePath + 'packages/edit/submit', query_str, function(data) {
        if (data != '') {
            if (oid != 0) {
                var e_tr = jQuery('#o_item_' + oid);
                var table_id = e_tr.parents('table').attr('id');
                var pos = window[table_id].fnGetPosition(e_tr[0]);
                window[table_id].fnUpdate(data, pos); // Row
                tvn_order_bind_edit_action();
            }
            jQuery("#dialog_box").dialog('close');
        } else {
            alert('Could not save data.Please try again!');
        }
    });
}
function tvn_order_bind_edit_action() {
    if (jQuery('.order-dialog-edit').length > 0) {
        jQuery('.order-dialog-edit').bind('click', function() {
            var pid = jQuery(this).attr('pid');
            jQuery.get(Drupal.settings.basePath + 'packages/edit/form/' + pid, {}, function(data) {
                if (data != '') {
                    jQuery("#dialog_box").html(data);
                    jQuery("#dialog_box").dialog({
                        autoOpen: false,
                        width: 700,
                        modal: true,
                        buttons: {
                            Remove: function() {
                                jQuery(this).dialog('close');
                            },
                            Save: function() {
                                if (tvn_order_edit_validate('order_edit_field')) {
                                    tvn_order_edit_submit('order_edit_field');
                                }
//                                jQuery(this).dialog('close');
                            },
                            Cancel: function() {
                                jQuery(this).dialog('close');
                            }
                        }
                    });
                    jQuery("#dialog_box").dialog('open');
                } else {
                    alert('Could not get data.');
                }
            });
        });
    }
}